<!-- 猜你喜欢通用组件 -->
<script setup lang="ts">
//引入类型
import type { PageParams } from "@/types/global";
import type { GuessItem } from "@/types/home";
//@ts-ignore,引入请求函数
import { reqHomeGoodsGuessLike } from "@/services/home.ts";
import { onMounted, ref } from "vue";

//猜你喜欢数据
let guessList = ref<GuessItem[]>([]);
//猜你喜欢数据页面是否全部加载完
const finish = ref(false);
//请求猜你喜欢api参数
const pageParams:Required<PageParams> = {
    page:1,
    pageSize:10
}

//生命周期
onMounted(() => {
    //请求数据
    getGuessLikeList();
});

//请求猜你喜欢数据
const getGuessLikeList = async () => {
    if(finish.value == true) return uni.showToast({icon:"none",title:"没有更多数据!"});
    let res = await reqHomeGoodsGuessLike(pageParams);
    guessList.value.push(...res.result.items);
    //判断是否还有下一页数据
    if(pageParams.page < res.result.pages){
        pageParams.page ++;
    } else {
        finish.value = true;
    }
    console.log("猜你喜欢api",res);
} 

//用户在首页下拉更新数据,需要将里面的请求参数、标记置回初始值
const resetData = () => {
  pageParams.page = 1;
  guessList.value = [];
  finish.value = false;
}

//对外暴露数据、方法
defineExpose({
    getGuessLikeList,
    resetData
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator class="guess-item" v-for="item,index in guessList" :key="index" :url="`/pages/goods/goods?id=${item.id}`">
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name">{{ item.name }}</view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text"> {{ finish ? '没有更多数据~' : '正在加载...' }} </view>
</template>

<style lang="scss">
:host {
  display: block;
}
/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;
  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}

/* 猜你喜欢 */
.guess {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;
  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
    background-color: #fff;
  }
  .image {
    width: 304rpx;
    height: 304rpx;
  }
  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }
  .small {
    font-size: 80%;
  }
}
// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>